package com.weiyi.portal.controller;

import com.weiyi.common.common.api.CommonResult;
import com.weiyi.portal.dto.NewsDTO;
import com.weiyi.portal.modules.ums.model.UmsNews;
import com.weiyi.portal.modules.ums.service.UmsNewsService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "PushController", description = "动态发布Controller")
@RequestMapping("/pushHome")
public class PushController {
    @Autowired
    UmsNewsService umsNewsService;

    @RequestMapping("/pushNews")
    public CommonResult pushNews(@RequestBody NewsDTO newsDTO){
        UmsNews umsNews = new UmsNews();
        BeanUtils.copyProperties(newsDTO, umsNews);
        umsNews.setShowStatus(1);

        Boolean flag = umsNewsService.saveNews(umsNews);
        if(flag)return  CommonResult.success(flag);
        return  CommonResult.failed();
    }

    //待改进
    @RequestMapping("/saveNews")
    public CommonResult saveNews(@RequestBody NewsDTO newsDTO){
        UmsNews umsNews = new UmsNews();
        BeanUtils.copyProperties(newsDTO, umsNews);
        umsNews.setShowStatus(0);

        Boolean flag = umsNewsService.saveNews(umsNews);
        if(flag)return  CommonResult.success(flag);
        return  CommonResult.failed();
    }

    @RequestMapping("/getDraftByUserId")
    public CommonResult getDraftByUserId(@RequestBody Integer id){
        List<UmsNews> draft = umsNewsService.getDraft(id);
        return CommonResult.success(draft);
    }
}
